﻿using System;
using System.Windows.Media;

namespace Candy.Infrastructure
{
    public static class HeatMapper
    {

        private const int steps = 255;
        public static Color MapValue(double val, double min, double max, Color minColor, Color maxColor)
        {
            return HeatMapper.MapValue(val, min, max, minColor, maxColor, 255);
        }

        public static Color MapValue(double val, double min, double max, Color minColor, Color maxColor, int alphaChannel)
        {
            if (max < min)
                throw new ArgumentException("The Min can't be greate than the Max");

            double valx = Math.Max(Math.Min(val, max), min);

            double rDiff = maxColor.R - minColor.R;
            double gDiff = maxColor.G - minColor.G;
            double bDiff = maxColor.B - minColor.B;

            double rStep = rDiff / HeatMapper.steps;
            double gStep = gDiff / HeatMapper.steps;
            double bStep = bDiff / HeatMapper.steps;

            double factor = (max - min) / HeatMapper.steps;

            double x = Math.Max(0, Math.Min(255,(valx - min) / factor));

            int r = (int)(minColor.R + Math.Floor((double)(rStep * x)));
            int g = (int)(minColor.G + Math.Floor((double)(gStep * x)));
            int b = (int)(minColor.B + Math.Floor((double)(bStep * x)));
            Color c = minColor;
            try
            {
                c = Color.FromArgb(Convert.ToByte(alphaChannel), Convert.ToByte(r), Convert.ToByte(g), Convert.ToByte(b));
            }
            catch (Exception ae )
            {
                string s = ae.Message;
            }
            return c;
        }



    }
}
